textpatt = '''
z = round(random(%2.2f)-.41)
r = 999
s = '%s'
if(global.debug==1||getoption('roompick')==1) z = get_integer(s,0)
switch(z){
%s
}
if(r=999) show_error('Invalid Number!',1)
room_goto(r)
'''
codepatt = 'case %d: r=%s;break;\n'
helppatt = '%d- %s '

in_file = open('cannonsrooms.txt','r')
out_file = open('out.txt','w')
oldlines = in_file.readlines()

lines = []
codestring = ''
helpstring = ''

for line in oldlines:
    if line[0] != '#':
        lines.append(line)

numb = len(lines)

i = 0
j = 100
for line in lines:
    line = line.replace('\n','')
    frmtline = line.replace('_',' ')
    frmtline = frmtline.title()
    if line[0] == '$':
        numb -= 1
        helpstring += helppatt % (j, frmtline.replace('$',''))
        codestring += codepatt % (j, line.replace('$',''))
        j += 1
    else:
        helpstring += helppatt % (i, frmtline)
        codestring += codepatt % (i, line)
        i += 1

text = textpatt % ( numb - .1 , helpstring, codestring)
out_file.write(text)
